package com.action;

import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.beans.StudentInfo;

@Controller
public class LoginAction {

	//用于国际化
	@Resource
	private MessageSource messageSource; 
	
	@PostMapping("/login")
	public String login(String userName,String password,ModelMap m,HttpSession ss)
	{
		if("admin".equals(userName) && "123".equals(password)) {
			StudentInfo s=new StudentInfo();
			s.setStuName(userName);
			s.setPassword(password);
			ss.setAttribute("session_student", s);
			return "main";
		}
		else {		
			Locale locale=LocaleContextHolder.getLocale();  //得到当前的语言环境
			String msg =messageSource.getMessage("loginerror", null,locale);   //loginerror 是写在资源文件中的某条信息的key

			m.put("msg",msg);
			return "login"; 
		}
	}
	
}
